package com.system.controller;

import com.system.po.AccidentType;
import com.system.po.Plan;
import com.system.service.AccidentService;
import com.system.service.PlanService;
import com.system.utils.MakePlanTree;
import com.system.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("plan")
@Api(description = "计划处理接口")
public class PlanController {
    @Autowired
    private PlanService planServiceImpl;
    @Autowired
    private AccidentService accidentServiceImpl;
    //测试访问数据库
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ApiOperation(value = "树形list")
    @ResponseBody
    public Result selectPlanList() throws Exception {

     List<Plan> planList=  planServiceImpl.selectPlanList();
        planList=MakePlanTree.makeTree(planList,0);
        for (int i=0;planList.size()<=0;i++){
            planList=MakePlanTree.makeTree(planList,i);
        }

        return planList.size()>0?Result.ok(planList):Result.fail("查询失败或没有数据");
    }


    @RequestMapping(value = "/accidentType/{id}", method = {RequestMethod.GET})
    @ApiOperation(value = "根据id查询并返回数据和转发到指定的admin/showEditPlan页面")
    public ModelAndView selectPlanByPlanIdAndReturnAccidentTypeList(@PathVariable(value = "id",required = true) Integer id) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.setViewName("admin/showEditPlan");
        Plan plan=  planServiceImpl.selectPlanByPlanId(id);

        mv.addObject("plan",plan);
        List<AccidentType> accidentList= accidentServiceImpl.selectAccidentList();
        mv.addObject("accidentList",accidentList);
        return  mv;
    }



    @RequestMapping("/editPlan/{id}")
    @ApiOperation("根据id查询plan和accidentList数据并转发到admin/editPlan页面")
    public ModelAndView editPlan(@PathVariable("id") Integer id) throws Exception {
        ModelAndView mv=new ModelAndView();
        mv.setViewName("admin/editPlan");
        Plan plan=  planServiceImpl.selectPlanByPlanId(id);

        mv.addObject("plan",plan);
        List<AccidentType> accidentList= accidentServiceImpl.selectAccidentList();
        mv.addObject("accidentList",accidentList);

        return mv;
    }

    @RequestMapping( method = {RequestMethod.PUT})
    @ApiOperation(value = "修改")
    @ResponseBody
    public Result  updatePlanByPlanId(@RequestBody Plan plan) throws Exception {
        Boolean planByid=  planServiceImpl.updatePlanByPlanId(plan);
        return  planByid?Result.ok("修改成功"):Result.fail("修改失败");
    }

    @RequestMapping( method = {RequestMethod.POST})
    @ApiOperation(value = "添加")
    @ResponseBody
    public String  addPlan(@RequestBody Plan plan) throws Exception {

        Boolean planByid=  planServiceImpl.addPlan(plan);
        return  planByid?"添加成功":"添加失败";
    }
    @RequestMapping(value = "/add" ,method = {RequestMethod.POST})
    @ApiOperation(value = "添加并返回主键")
    @ResponseBody
    public Result  addPlanAndReturnPlanId(@RequestBody Plan plan) throws Exception {
        Plan planByid=  planServiceImpl.addPlanAndReturnPlanId(plan);
        return  planByid!=null?Result.ok(planByid):Result.fail("添加失败");
    }


    @RequestMapping(value = "/{id}" ,method = {RequestMethod.DELETE})
    @ApiOperation(value = "删除数据")
    @ResponseBody
    public Result  addPlanAndReturnPlanId(@PathVariable Integer id) throws Exception {

        Boolean deleteplanByid=  planServiceImpl.deletePlanById(id);
        return  deleteplanByid?Result.ok("删除成功"):Result.fail("删除失败");
    }


    @RequestMapping(value = "/children/{id}" ,method = {RequestMethod.GET})
    @ApiOperation(value = "根据父节点parent_id获取子数据")
    @ResponseBody
    public Result  selectPlanListChildrenByParentId(@PathVariable Integer id) throws Exception {

        List<Plan> planList=  planServiceImpl.selectPlanListChildrenByParentId(id);
        return  planList.size()>0?Result.ok(planList):Result.fail("查询失败");
    }

    @RequestMapping(value = "/children/{id}" ,method = {RequestMethod.POST})
    @ApiOperation(value = "根据当前选中的节点添加子数据,并设置父id")
    @ResponseBody
    public Result  addPlanChildrenByParentIdAndSetParentId(@PathVariable Integer id,@RequestBody Plan plan) throws Exception {
        Plan p=  planServiceImpl.selectPlanByPlanId(id);
        if(p==null){
            throw new NullPointerException("对象不存在");
        }

        //添加子节点就是把当前点击的id设置为parentId父id
        plan.setParentId(id);
        Boolean add=  planServiceImpl.addPlan(plan);
        return  add?Result.ok("添加成功"):Result.fail("添加失败");
    }

    @RequestMapping(value = "/brother/{id}" ,method = {RequestMethod.POST})
    @ApiOperation(value = "根据当前选中的节点添加兄弟节点数据,并设置父他的ParentId为我的ParentId")
    @ResponseBody
    public Result addPlanbrotherByParentIdAndSetParentId(@PathVariable("id") Integer id, @RequestBody Plan plan) throws Exception {
        Plan p=  planServiceImpl.selectPlanByPlanId(id);
        if(p==null){
            throw new NullPointerException("对象不存在");
        }
        //添加兄弟节点就是把当前点击的节点的parentId父id设置为parentId
        plan.setParentId(p.getParentId());
        Boolean add=  planServiceImpl.addPlan(plan);
        return  add?Result.ok("添加成功"):Result.fail("添加失败");
    }


    // 转发地点管理
    @RequestMapping( value = "/editPlace")
    @ApiOperation(value = "转发地点管理")
    @ResponseBody
    public ModelAndView editPlace() throws Exception {
        ModelAndView mv2=new ModelAndView();
        mv2.setViewName("admin/editPlace");
        return mv2;
    }
    // 转发专用图纸
    @RequestMapping( value = "/drawing")
    @ApiOperation(value = "转发图纸管理")
    @ResponseBody
    public ModelAndView drawing() throws Exception {
        ModelAndView mv2=new ModelAndView();
        mv2.setViewName("admin/drawing");
        return mv2;
    }

    @RequestMapping( value = "/disaster_Relief_Analysis")
    @ApiOperation(value = "转发救灾分析页面")
    public String disaster_Relief_Analysis() throws Exception {
        return "admin/disaster_Relief_Analysis";
    }



    @RequestMapping( value = "/disaster_Relief_Analysis{id}")
    @ApiOperation(value = "转发救灾分析页面")
    public String disaster_Relief_Analysis2(@PathVariable Long id) throws Exception {
        return "admin/disaster_Relief_Analysis"+id;
    }

    // 转发事图纸预览
    @RequestMapping( value = "/pdf")
    @ApiOperation(value = "角色权限")
    @ResponseBody
    public ModelAndView pdf() throws Exception {
        ModelAndView mv2=new ModelAndView();
        mv2.setViewName("admin/pdf");
        return mv2;
    }


}
